// pages/deliveryAddr/deliveryAddr.js\
var common = require("../../utils/Conmon.js");
var appData = getApp().globalData;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    From: '',
    ArrayList: null,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    common.pageInit(function() {});
    var _from = options.from;
    that.setData({
      From: _from
    })
    wx.showLoading({
      title: '加载中...',
      mask: true
    });
    that.wxSubRequest('GET', 'member/api/Address/AddressList', {
        userId: wx.getStorageSync('UserInfo').UserID,
        custId: appData.custID
      },
      function(data) {
        if (data.code == 1200) {
          wx.hideLoading()
          that.setData({
            ArrayList: data.content
          })
        }
      },
      function(obj) {
        wx.hideLoading()
        console.log(obj)
      })
  },

  /**
   * 绑定获取收获地址信息
   */
  bindGetAddressTap: function(event) {
    var that = this;
    var cneeId = event.currentTarget.id;
    if (that.data.From == 'order') {
      //来源订单跳提交订单页
      wx.redirectTo({
        url: '/pages/submitOrder/submitOrder?CneeID=' + cneeId,
      })
    } else if (that.data.From == 'team') {
      //来源拼团订单跳拼团提交订单页
      wx.redirectTo({
        url: '/pages/teamSubmitOrder/teamSubmitOrder?CneeID=' + cneeId,
      })
    } else {
      //跳转到编辑页
      wx.redirectTo({
        url: '/pages/addAddr/addAddr?CneeID=' + cneeId,
      })
    }
  },

  /**
   * 删除地址
   */
  bindDelAddressTap: function(event) {
    var that = this;
    var cneeId = event.currentTarget.id;
    var param = {
      custId: appData.custID,
      Id: cneeId
    };
    wx.showModal({
      title: '提示',
      content: '确定要删除吗？',
      success: function(res) {
        if (res.confirm) {
          wx.showLoading({
            title: '提交中...',
            mask: true
          });
          that.wxSubRequest('GET', 'member/api/Address/Del', param,
            function(data) {
              if (data.code == 1200) {
                wx.hideLoading()
                //获取列表中要删除项的下标  
                var index = event.currentTarget.dataset.index;
                var list = that.data.ArrayList;
                //移除列表中下标为index的项  
                list.splice(index, 1);
                //更新列表的状态  
                that.setData({
                  ArrayList: list
                });
              }
            },
            function(obj) {
              wx.hideLoading()
              wx.showModal({
                title: '错误',
                content: '删除失败',
                showCancel: false
              })
              console.log(obj)
            })
        }
      },
      fail: function(res) {
        console.log(res.data)
      }
    })

  },

  /**
   * 编辑地址信息
   */
  bindEditAddressTap: function(event) {
    var that = this;
    var cneeId = event.currentTarget.id;
    var url = '/pages/addAddr/addAddr?CneeID=' + cneeId;
    if (that.data.From) {
      url = url + "&from=" + that.data.From;
    }
    //跳转到编辑页
    wx.redirectTo({
      url: url,
    })
  },

  /**
   * 绑定新增地址事件
   */
  bindAddTap: function(event) {
    var that = this;
    var url = '/pages/addAddr/addAddr?CneeID=0';
    if (that.data.From) {
      url = url + "&from=" + that.data.From;
    }
    //跳转到编辑页
    wx.redirectTo({
      url: url,
    })
  },

  /**
   * 设置默认地址
   */
  setDefaultAddressTap: function(event) {
    var that = this;
    var cneeId = event.currentTarget.id;
    var param = {
      custId: appData.custID,
      cneeId: cneeId
    };
    wx.showLoading({
      title: '正在设置...',
      mask: true
    });
    that.wxSubRequest('GET', 'member/api/Address/SetDefault', param,
      function(data) {
        if (data.code == 1200) {
          wx.hideLoading()
          var urlRoute = "deliveryAddr";
          if (that.data.From == 'order') {
            urlRoute = "/pages/submitOrder/submitOrder?CneeID=" + cneeId
          } else if (that.data.From == 'team') {
            urlRoute = "/pages/teamSubmitOrder/teamSubmitOrder?CneeID=" + cneeId
          }
          wx.redirectTo({
            url: urlRoute,
          })
        }
      },
      function(obj) {
        wx.hideLoading()
        wx.showModal({
          title: '错误',
          content: '地址设置失败',
          showCancel: false
        })
        console.log(obj)
      })
  },

  /**
   * 微信提交请求获取数据或保存数据
   * 
   */
  wxSubRequest: function(method, url, param, callBack, CallBackError) {
    wx.request({
      url: appData.apiurl + url,
      method: method,
      data: param,
      header: {
        "content-type": "application/json",
        'X_ManuName': appData.manuName
      },
      success: function(res) {
        if (res.statusCode == 200 && res.data.code == 1200) {
          var data = res.data;
          callBack(data)
          console.log(res.data)
        }
      },
      fail: function(res) {
        CallBackError(res.data)
        console.log(res.data)
      },
      complete: function(res) {
        console.log(res.msg)
      }
    })
  },

  /**************************************************************/

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})